package cn.iflags.Mall.Model;

import cn.iflags.Mall.Chacke.OneMinuteTokenChache;
import cn.iflags.Mall.View.PasteView;
import cn.iflags.Mall.Chacke.OneHourTokenChache;
import cn.iflags.Mall.Chacke.FiveMinuteTokenChache;
import cn.iflags.Mall.common.ServerResponse;
import cn.iflags.Mall.dao.PasteMapper;
import cn.iflags.Mall.pojo.Paste;
import cn.iflags.Mall.service.IPasteService;
import cn.iflags.Mall.util.*;
import cn.iflags.Mall.vo.PasteVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.UUID;

/**
 * 描述:代码贴处理模型
 * @author Vincent Vic
 * create 2020-03-13 10:20
 */

@Controller
@RequestMapping("/paste/")   //请求地址设置
public class PasteModel {

    private static Logger log = LoggerFactory.getLogger(PasteModel.class);

    @Autowired
    private PasteMapper pasteMapper;

    @Autowired
    private IPasteService iPasteService;

    //上传代码信息
    @RequestMapping(value = "up/upload.do",method = RequestMethod.POST)
    @ResponseBody   //自动序列化Json
    public ModelAndView uploadPaste(HttpServletRequest session, String codeData, Paste paste){
        String ip = HttpUtil.getRemoteIp(session);
        //代码贴页面
        PasteView pasteView = new PasteView();
        //判断是否在同一分钟内生成 先判断是否在限制缓冲池里面
        String token = FiveMinuteTokenChache.getKey(OneHourTokenChache.getTokenFprefix()+ip + DateTimeUtil.getAnyByToday("ddhh"));
        if (!StringUtils.isBlank(token)){   //  如果一分钟内重复提交限制
            return pasteView.errorPaste("操作太频繁了-等五分钟再试");
        }

        //server->mybatis->dao
        log.info(paste.getSyntax()+","+paste.getStyle()+","+paste.getTitleName()+","+paste.getDetail());
        //调用上传服务，成功返回网页key，不成功返回错误信息
        ServerResponse response = iPasteService.uploadPaste(ip,paste,codeData);

        if (!response.isSeccess()){ //不成功跳转回代码贴页面
            log.info(response.getMsg());
            return pasteView.errorPaste(response.getMsg());
        }
        /**
         * 限制方案：
         * 一分钟请求次数大于三次加入限制池
         */
        //完成操作 更新缓存检测池情况
        token = OneMinuteTokenChache.getKey(OneHourTokenChache.getTokenFprefix()+ip + DateTimeUtil.getAnyByToday("ddhh"));
        int count = 1;
        if (!StringUtils.isBlank(token) ) { //如果检测池存在记录，记录值累加
            count +=Integer.valueOf(token);
        }
        //进行累加或者初始化
        OneMinuteTokenChache.setKey(OneHourTokenChache.getTokenFprefix() + ip + DateTimeUtil.getAnyByToday("ddhh"), String.valueOf(count));
        //判断是否已经达到限制，如果达到限制设置限制缓冲池
        if (count >= 3) {
            //设置时效缓存
            FiveMinuteTokenChache.setKey(OneHourTokenChache.getTokenFprefix() + ip + DateTimeUtil.getAnyByToday("ddhh"), UUID.randomUUID().toString());

        }//成功跳转到显示页面
        return pasteView.gotoPasteShow(response.getMsg());
    }

    //请求展示页面
    @RequestMapping(value = "p/{key}",method = RequestMethod.GET)
    public ModelAndView getPaste(HttpSession session, @PathVariable String key){
        log.info("/p/key+拦截信息:"+key);
        PasteView pasteView = new PasteView();
        int row = pasteMapper.checkKey(key);
        if (row <= 0 ) {
            log.error("页不存在");
            return pasteView.errorPaste("页面不存在");
        }
        ServerResponse response = iPasteService.getPaste(key);
        if (!response.isSeccess()){ //不成功跳转回代码贴页面
            return pasteView.errorPaste(response.getMsg());
        }
        //成功跳转到显示页面
        return pasteView.show((PasteVo) response.getData());
    }

}